<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CommandChangeUnionRate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ChangeUnionRate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更改某个公会下所有成员对私分成';

    protected $manageCon = 'mysql';

    protected $apiCon = 'api';
    protected $unionTable = 'union';
    protected $unionUserTable = 'union_user';
    protected $unionRateTable = 'union_user_rate';

    protected $log = 'change_union_rate';
    protected $error_log = 'error_change_union_rate';
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $unionId = '1283';
        $rate = '55';//整数

        //查询公会信息
        do{
            $unionInfo = DB::connection($this->apiCon)->table($this->unionTable)->where('id',$unionId)->first();
            $unionInfo = objToArray($unionInfo);
            if(empty($unionInfo)){
                write_log($this->log,'INFO','无工会信息');
                break;
            }
            if($unionInfo['authentication_status'] != 1){
                write_log($this->log,'INFO','非对私公会信息');
                break;
            }
            //查询公会成员
            $unionUser = DB::connection($this->apiCon)->table($this->unionUserTable)->where('un_id',$unionId)->where('user_id','!=',$unionInfo['un_user_id'])->get(['user_id']);
            $unionUser = objToArray($unionUser);
            if(empty($unionUser)){
                write_log($this->log,'INFO','无工会成员信息');
                break;
            }
            //循环处理公会成员费率
            DB::beginTransaction();
            try{
                foreach($unionUser as $v){
                    $check = DB::connection($this->apiCon)->table($this->unionRateTable)->where(['un_id'=>$unionId,'user_id'=>$v['user_id']])->first();
                    $check = objToArray($check);
                    if(!empty($check)){
                        //更新原来的费率
                        $update = [
                            'rate'=>$rate,
                            'confirm_status' => 1,
                            'update_time'=>date('Y-m-d H:i:s',time()),
                        ];
                        DB::connection($this->apiCon)->table($this->unionRateTable)->where('id',$check['id'])->update($update);
                    }else{
                        $data = [
                            'rate'=>$rate,
                            'un_id'=>$unionId,
                            'user_id'=>$v['user_id'],
                            'confirm_status' => 1,
                            'create_time'=>date('Y-m-d H:i:s',time()),
                            'update_time'=>date('Y-m-d H:i:s',time())
                        ];
                        DB::connection($this->apiCon)->table($this->unionRateTable)->insert($data);
                    }
                    DB::commit();
                    write_log($this->log,'INFO','公会'.$unionInfo['name'].'-成员'.$v['user_id'].'更改/新增费率'.$rate.'成功'); 
                }
            }catch(\Exception $e){
                write_log($this->error_log,'INFO',['msg'=>'公会'.$unionInfo['name'].'-成员'.$v['user_id'].'更改/新增费率'.$rate.'失败','error'=>$e->getMessage(),'line'=>$e->getLine(),'file'=>$e->getFile()]); 
                DB::rollBack();
            }
            
        }while(false);
    }
}